package com.approval.approvalapp.message;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.MyAppliaction;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.MessageListBean;
import com.approval.approvalapp.bean.NoticeBean;
import com.approval.approvalapp.bean.WtrBean;
import com.approval.approvalapp.index.dbsx.tshf.BaseActivity2;
import com.approval.approvalapp.message.adapter.ChrvAdapter;
import com.approval.approvalapp.message.adapter.WtrAdapter;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.DownResponseInterface;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.google.gson.JsonObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MessageXqActivity extends BaseActivity2 implements WtrAdapter.ClickListener {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.tv2)
    TextView tv2;
    @Bind(R.id.tv3)
    TextView tv3;
    @Bind(R.id.tv4)
    TextView tv4;
    @Bind(R.id.file_title_tv)
    TextView fileTitleTv;
    @Bind(R.id.file_ll)
    LinearLayout fileLl;
    @Bind(R.id.bottom_rl)
    RelativeLayout bottom_rl;
    @Bind(R.id.bottom_rl2)
    RelativeLayout bottom_rl2;
    @Bind(R.id.chry_ll)
    LinearLayout chry_ll;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Bind(R.id.rvtitle_ll)
    LinearLayout rvtitle_ll;
    @Bind(R.id.sbjsxzs)
    TextView sbjsxzs;
    @Bind(R.id.gray_view)
    View grayView;
    @Bind(R.id.close)
    ImageView close;
    @Bind(R.id.ll1)
    LinearLayout ll1;
    @Bind(R.id.iv1)
    ImageView iv1;
    @Bind(R.id.qrchtv1)
    TextView qrchtv1;
    @Bind(R.id.iv2)
    ImageView iv2;
    @Bind(R.id.qrchtv2)
    TextView qrchtv2;
    @Bind(R.id.wtr_et)
    TextView wtrEt;
    @Bind(R.id.lll1)
    LinearLayout lll1;
    @Bind(R.id.phone_et)
    EditText phoneEt;
    @Bind(R.id.ll2)
    LinearLayout ll2;
    @Bind(R.id.agree_tv)
    LinearLayout agreeTv;
    @Bind(R.id.root)
    LinearLayout root;
    @Bind(R.id.choolse_down)
    RelativeLayout choolse_down;
    @Bind(R.id.close2)
    ImageView close2;
    @Bind(R.id.root2)
    LinearLayout root2;
    @Bind(R.id.rv2)
    RecyclerView rv2;
    @Bind(R.id.close3)
    ImageView close3;
    @Bind(R.id.bs_et)
    EditText bsEt;
    @Bind(R.id.agree_tv2)
    LinearLayout agreeTv2;
    @Bind(R.id.root3)
    LinearLayout root3;
    private String uid;
    private NoticeBean noticeBean;
    boolean isch = true;
    private List<WtrBean> wtrBeans;
    private MessageListBean messageListBean;
    private boolean gone;

    private String urlwf = "v1/user/notice_show";//未发
    private String urlyf = "v1/user/notice_shows";//已发
    private String url;


    @Override
    public int getLayoutId() {
        return R.layout.activity_message_xq;
    }

    @Override
    public void initView() {
        messageListBean = (MessageListBean) getIntent().getSerializableExtra("bean");
        gone = getIntent().getBooleanExtra("gone", false);


        refresh(messageListBean);
    }

    private void refresh(MessageListBean messageListBean) {
        JSONObject jsonObject = new JSONObject();
        if (gone) {
            //从已经发送的过来
            jsonObject.put("id", messageListBean.getId());
            url = urlyf;
        }else {
            jsonObject.put("id", messageListBean.getId());
            jsonObject.put("notice_id", messageListBean.getNotice_id());
            jsonObject.put("name", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.UNIT_NAME, ""));
            jsonObject.put("IFJS", messageListBean.getIFJS());
            jsonObject.put("TZTYPE", messageListBean.getTZTYPE());
            url = urlwf;
        }


        NetRequsetUtil.getInstance().netRequestPostJson(url, jsonObject.toString(), new NetDownResponse() {
            @Override
            public void success(String str) {
                noticeBean = JSON.parseObject(str, NoticeBean.class);
                if (tv1 == null ){
                    return;
                }
                tv1.setText(noticeBean.getTitle());
                tv2.setText(noticeBean.getAuthor());
                tv3.setText(noticeBean.getAdd_time());
                tv4.setText(noticeBean.getContent());


                if (TextUtils.isEmpty(noticeBean.getFile_url())) {
                    fileLl.setVisibility(View.GONE);
                } else {
                    fileLl.setVisibility(View.VISIBLE);
                    fileTitleTv.setText(noticeBean.getFile_name());
                }

                if (gone) {
                    bottom_rl.setVisibility(View.GONE);
                    bottom_rl2.setVisibility(View.GONE);
                } else {
                    if (noticeBean.getTZTYPE() == 0 && noticeBean.getIFCH() == 0) {
                        bottom_rl.setVisibility(View.VISIBLE);
                    } else {
                        bottom_rl.setVisibility(View.GONE);
                    }

                    if (noticeBean.getTZTYPE() == 1 && noticeBean.getIFHF() == 1) {
                        bottom_rl2.setVisibility(View.VISIBLE);
                    } else {
                        bottom_rl2.setVisibility(View.GONE);
                    }
                }

                if (noticeBean.getTZTYPE() == 0) {
                    chry_ll.setVisibility(View.VISIBLE);
                    rvtitle_ll.setVisibility(View.VISIBLE);
                    rv.setVisibility(View.VISIBLE);
                } else {
                    chry_ll.setVisibility(View.GONE);
                    rvtitle_ll.setVisibility(View.GONE);
                    rv.setVisibility(View.GONE);
                }

                List<NoticeBean.MemberListBean> member_list = noticeBean.getMember_list();

                rv.setLayoutManager(new LinearLayoutManager(MessageXqActivity.this));
                ChrvAdapter chrvAdapter = new ChrvAdapter();
                chrvAdapter.setList(member_list);
                rv.setAdapter(chrvAdapter);

            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {

            }
        });
    }

    @Override
    public void initData() {

    }

    private WtrAdapter wtrAdapter;


    @OnClick({R.id.back_iv, R.id.file_ll, R.id.bottom_rl, R.id.iv1, R.id.iv2, R.id.close
            , R.id.choolse_down, R.id.close2, R.id.agree_tv, R.id.bottom_rl2, R.id.close3,
            R.id.agree_tv2})
    public void onViewClicked(View view) {
        JSONObject jsonObject = new JSONObject();
        switch (view.getId()) {
            case R.id.agree_tv2:
                if (bsEt.getText().toString().length() == 0) {
                    Toast.makeText(this, "请输入报送内容", Toast.LENGTH_SHORT).show();
                    return;
                }
                jsonObject.put("notice_id", noticeBean.getId());
                jsonObject.put("content", bsEt.getText().toString());
                jsonObject.put("name", SpUtil.getString(this, SpUtil.USER_RUN_NICKNAME, ""));
                NetRequsetUtil.getInstance().netRequestPostJson("v1/user/submit_meeting_fk", jsonObject.toJSONString(),
                        new NetDownResponse() {
                            @Override
                            public void success(String str) {
                                refresh(messageListBean);
                                root3.setVisibility(View.GONE);
                                grayView.setVisibility(View.GONE);
                                if (MessageFragment.instance != null){
                                    MessageFragment.instance.refreshData();
                                }
                            }

                            @Override
                            public void errowithresponse(String str) {
                                ErrorBeanUtil.getErrorMessage(str);
                            }

                            @Override
                            public void erro() {
                                ErrorBeanUtil.getErrorMessage(null);
                            }

                            @Override
                            public void finish() {

                            }
                        });
                break;
            case R.id.close3:
                root3.setVisibility(View.GONE);
                grayView.setVisibility(View.GONE);
                break;
            case R.id.bottom_rl2:
                grayView.setVisibility(View.VISIBLE);
                root3.setVisibility(View.VISIBLE);
                break;
            case R.id.agree_tv:
                jsonObject.put("notice_id", noticeBean.getId());
                jsonObject.put("name", SpUtil.getString(this,
                        SpUtil.USER_RUN_NICKNAME, ""));
                if (!isch) {
                    if (TextUtils.isEmpty(uid)) {
                        Toast.makeText(this, "请选择委托人", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    jsonObject.put("IFCH", "1");
                    jsonObject.put("WTUSERID", uid);
                    jsonObject.put("WTNAME", wtrEt.getText().toString());
                    jsonObject.put("WTTEL", phoneEt.getText().toString());
                } else {
                    jsonObject.put("IFCH", "0");
                }
                Log.d("MessageXqActivity", jsonObject.toString());
                NetRequsetUtil.getInstance().netRequestPostJson("v1/user/submit_is_meeting",
                        jsonObject.toString(),
                        new NetDownResponse() {
                            @Override
                            public void success(String str) {
                                refresh(messageListBean);
                                grayView.setVisibility(View.GONE);
                                root.setVisibility(View.GONE);
                                if (MessageFragment.instance != null){
                                    MessageFragment.instance.refreshData();
                                }
                            }

                            @Override
                            public void errowithresponse(String str) {
                                ErrorBeanUtil.getErrorMessage(str);

                            }

                            @Override
                            public void erro() {
                                ErrorBeanUtil.getErrorMessage(null);
                            }

                            @Override
                            public void finish() {

                            }
                        });
                break;
            case R.id.close2:
                root2.setVisibility(View.GONE);
                break;
            case R.id.choolse_down:
                root2.setVisibility(View.VISIBLE);
                if (wtrBeans != null && wtrBeans.size() > 0) {
                    wtrAdapter.setList(wtrBeans, uid, MessageXqActivity.this);
                    return;
                }
                jsonObject.put("org_code", SpUtil.getString(this, SpUtil.ORG_CODE, ""));
                NetRequsetUtil.getInstance()
                        .netRequestPostJson("v1/user/get_wtr_list", jsonObject.toString(), new NetDownResponse() {


                            @Override
                            public void success(String str) {
                                wtrBeans = JSON.parseArray(str, WtrBean.class);

                                rv2.setLayoutManager(new LinearLayoutManager(MessageXqActivity.this));
                                wtrAdapter = new WtrAdapter();
                                wtrAdapter.setList(wtrBeans, uid, MessageXqActivity.this);
                                rv2.setAdapter(wtrAdapter);
                            }

                            @Override
                            public void errowithresponse(String str) {

                            }

                            @Override
                            public void erro() {

                            }

                            @Override
                            public void finish() {

                            }
                        });
                break;
            case R.id.close:
                grayView.setVisibility(View.GONE);
                root.setVisibility(View.GONE);
                break;
            case R.id.iv1:
                if (isch) {
                    //已经是参会状态
                    return;
                } else {
                    iv1.setBackgroundResource(R.drawable.case_on);
                    iv2.setBackgroundResource(R.drawable.case_off);
                    qrchtv1.setTextColor(Color.parseColor("#FF1076FF"));
                    qrchtv2.setTextColor(Color.parseColor("#FF7D8198"));
                    lll1.setVisibility(View.GONE);
                    ll2.setVisibility(View.GONE);
                    isch = true;
                }
                break;
            case R.id.iv2:
                if (isch) {
                    iv2.setBackgroundResource(R.drawable.case_on);
                    iv1.setBackgroundResource(R.drawable.case_off);
                    tv2.setTextColor(Color.parseColor("#FF1076FF"));
                    qrchtv1.setTextColor(Color.parseColor("#FF7D8198"));
                    qrchtv2.setTextColor(Color.parseColor("#FF1076FF"));
                    lll1.setVisibility(View.VISIBLE);
                    ll2.setVisibility(View.VISIBLE);
                    isch = false;
                } else {
                    return;
                }
                break;
            case R.id.back_iv:
                finish();
                break;
            case R.id.file_ll:
                final ProgressDialog progressDialog = new ProgressDialog(this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setProgressNumberFormat(" ");
                progressDialog.setTitle("正在下载");
                progressDialog.setMessage("请稍候...");
                progressDialog.setProgress(0);
                progressDialog.setMax(100);
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();

                String url = noticeBean.getFile_url();
                NetRequsetUtil.getInstance().downLoadFile(this,
                        url,
                        url.substring((url.lastIndexOf("/") + 1), url.length()),
                        new DownResponseInterface() {
                            @Override
                            public void successFinish() {

                                progressDialog.dismiss();
                            }

                            @Override
                            public void progress(int progress) {

                                Log.d("WjFragprogress", "progress:" + progress);

                                progressDialog.setProgress(progress);
                            }

                            @Override
                            public void erro() {
                                progressDialog.dismiss();

                            }
                        }
                );
                break;
            case R.id.bottom_rl:
                grayView.setVisibility(View.VISIBLE);
                root.setVisibility(View.VISIBLE);
                break;
        }
    }


    @Override
    public void click(WtrBean wtrBean) {
        wtrEt.setText(wtrBean.getWTNAME());
        phoneEt.setText(wtrBean.getWTTEL());
        uid = wtrBean.getWTUSERID();
        root2.setVisibility(View.GONE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
